package com.ptmall.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ptmall.app.R;
import com.ptmall.app.bean.CategoryBean;
import com.ptmall.app.bean.HomePageTuijianBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.CateListActivity;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.NoScrollGridView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/1/12.
 */

public class HomepageTuijianAdapter extends BaseAdapter {

    private Context mContext;
    private List<HomePageTuijianBean> mDataList;
    private LayoutInflater mLayoutInflater;
    GoodListAdapter mGoodListAdapter;

    public HomepageTuijianAdapter(Context context, List<HomePageTuijianBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<HomePageTuijianBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_homepagetuijianlist,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        HomePageTuijianBean bean = mDataList.get(position);
        GlideUtil.showImg(mContext,bean.head_image,viewHolder.tuijianback);
        viewHolder.title.setText(bean.style_name);
        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, CateListActivity.class);
                intent.putExtra("id",bean.styleId);
                mContext.startActivity(intent);
            }
        });
        mGoodListAdapter = new GoodListAdapter(mContext,bean.productlist);
        viewHolder.freshgridview.setAdapter(mGoodListAdapter);
        viewHolder.freshgridview.setVisibility(View.GONE);
        viewHolder.freshgridview.setVisibility(View.VISIBLE);
        viewHolder.freshgridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, GoodDetilActivity.class);
//                intent.putExtra("id",bean.productlist.get(i).productId);
                mContext.startActivity(intent);
            }
        });
        mGoodListAdapter.setClickInterface(new GoodListAdapter.ClickInterface() {
            @Override
            public void shopcaradd(ImageView iv, String id, int position) {
                addcar(id,"1");

            }

        });
        return convertView;
    }

    class ViewHolder {

        TextView title,more;
        ImageView img,tuijianback;
        NoScrollGridView freshgridview;

        ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.title);
            more = (TextView) view.findViewById(R.id.more);
            freshgridview = (NoScrollGridView) view.findViewById(R.id.freshgridview);
            tuijianback = (ImageView) view.findViewById(R.id.tuijianback);
        }
    }


    private void addcar(String id,String num) {
        Map<String, String> map = new HashMap<>();
        map.put("productId", id);
        map.put("num", num);
        ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                Toast.makeText(mContext,"加入购物车成功",Toast.LENGTH_SHORT).show();
            }
        });
    }


}
